import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import styleImport from 'vite-plugin-style-import';
import path from 'path';
import html from 'vite-plugin-html';
import viteSvgIcons from 'vite-plugin-svg-icons';
import defaultSettings from './src/settings';

const title = defaultSettings.title;

// 为了在 vite.config.js中 获取环境变量
export default ({ mode }) => {
  // 获取环境变量
  const { VITE_BASE_API, VITE_TY_API } = loadEnv(mode, process.cwd());
  const port = 8086;
  // 设置正则
  const reg1 = new RegExp(`^\/${VITE_BASE_API}`);
  const reg2 = new RegExp(`^\/${VITE_TY_API}`);
  return defineConfig({
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        // 解决vue-i18n在开发环境下的告警
        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
      },
    },
    plugins: [
      vue(),
      /**
       *  注入环境变量到html模板中
       *  如在  .env文件中有环境变量  VITE_APP_TITLE=admin
       *  则在 html模板中  可以这样获取  <%- VITE_APP_TITLE %>
       *  文档：  https://github.com/anncwb/vite-plugin-html
       */
      html({
        inject: {
          injectData: {
            title: title
          },
        },
        minify: true,
      }),
      styleImport({
        libs: [{
          libraryName: 'element-plus',
          esModule: true,
          ensureStyleFile: true,
          resolveStyle: (name) => {
            name = name.slice(3)
            return `element-plus/packages/theme-chalk/src/${name}.scss`;
          },
          resolveComponent: (name) => {
            return `element-plus/lib/${name}`;
          },
        }]
      }),
      // 配置svg
      viteSvgIcons({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/icons')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
    server: {
      host: '0.0.0.0',
      port: port,
      open: true,
      proxy: {
        // jac项目管理api
        '/tysoft': {
          // target: 'http://10.8.18.202:9527', // API服务器的地址
          target: 'http://localhost:9527', // API服务器的地址
          // target: 'http://114.116.197.117/WebFoundation', //API服务器的地址
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/tysoft/, '')
        },
        // change xxx-api/login => mock/login
        // detail: https://cli.vuejs.org/config/#devserver-proxy
        // mock数据
        [VITE_BASE_API]: {
          target: `http://127.0.0.1:${port}/mock`,
          changeOrigin: true,
          rewrite: (path) => path.replace(reg1, '')
        },
        [VITE_TY_API]: {
          // target: `http://127.0.0.1`,
          target: 'http://59.173.253.146:11180',
          // target: `http://59.173.253.146:8888`,
          // target: `http://10.8.16.254:8888`,
          // target: `http://118.25.77.189:8001`,
          rewrite: (path) => path.replace(reg2, '')
        },
        // pdmAPI
        '/rest': {
          // target: 'http://localhost:8083',
          target: 'http://10.8.20.75:8083',
          // target: 'http://localhost:8083',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/rest/, '')
        }
      },
    }
  })
}

